package others;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 求一个数的开方。
 * @date 2020/4/2 13:54
 */
public class Sqrt {
    public static void main(String[] args) throws InterruptedException {
/*        double error = 1e-6;
        System.out.println(sqrt(2, error));
        System.out.println(Math.sqrt(2));
        System.out.println(sqrt(4, error));
        System.out.println(Math.sqrt(4));
        System.out.println(sqrt(0.01, error));
        System.out.println(Math.sqrt(0.01));*/
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 100, TimeUnit.HOURS,
                new LinkedBlockingDeque<>(1));
        executor.submit(()->{
            while (true);
        });
        System.out.println(executor.getActiveCount());
        System.out.println(executor.getQueue().size());
        executor.submit(()->{
            while (true);
        });
        System.out.println(executor.getActiveCount());
        System.out.println(executor.getQueue().size());
        Thread.sleep(1000);
        executor.submit(()->{
            while (true);
        });
        System.out.println(executor.getActiveCount());
        System.out.println(executor.getQueue().size());
        executor.submit(()->{
            while (true);
        });
        System.out.println(executor.getActiveCount());
        System.out.println(executor.getQueue().size());
    }
    public static double sqrt(double n,double error){
        if(n <= 0) return n == 0? 0 : -1;
        //采用二分法
        double left = n > 1? 1: n;
        double right = n > 1? n: 1;
        while (left <= right){
            double mid = (left + right)/2;
            double temp= mid*mid;
            //TODO 这里应该是temp根号误差，不是平房后的
            if(Math.abs(temp - n) <= error){
                return mid;
            }
            if(temp - n >0){
                right = mid;
            }else {
                left = mid;
            }
        }

        return -1;
    }
}
